package com.ruoyi.member.controller.portal;

import com.ruoyi.common.annotation.AllowAnonymous;
import com.ruoyi.common.context.AppUserContext;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.member.entity.req.MobileUserInfoReq;
import com.ruoyi.member.entity.req.WxMiniLoginReq;
import com.ruoyi.member.entity.resp.MobileUserInfoResp;
import com.ruoyi.member.entity.resp.WxMiniLoginResp;
import com.ruoyi.member.service.MobileUserService;
import com.ruoyi.member.service.WeChatMiniService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户
 */
@RestController
@RequestMapping("/portal/user/mobileUser")
public class MobileUserController {

    @Autowired
    private MobileUserService mobileUserService;
    @Autowired
    private WeChatMiniService weChatMiniService;

    @RequestMapping("/getWxAccessToken")
    public String getWxAccessToken() {
        return weChatMiniService.getWxAccessToken();
    }

    /**
     * 小程序登录
     */
    @PostMapping("/login")
    @AllowAnonymous
    @ResponseBody
    public R<WxMiniLoginResp> login(@RequestBody WxMiniLoginReq req) {
        return weChatMiniService.login(req);
    }

    /**
     * 编辑用户信息
     */
    @RequestMapping("/edit")
    public R<?> edit(@RequestBody MobileUserInfoReq req) {
        return mobileUserService.edit(req);
    }

    /**
     * 获取用户信息（允许未登录）
     */
    @RequestMapping("/getInfo")
    @AllowAnonymous
    public R<MobileUserInfoResp> getInfo(Long userId) {
        return mobileUserService.getInfo(userId);
    }

    /**
     * 获取用户信息（本人）
     */
    @RequestMapping("/getMyInfo")
    public R<MobileUserInfoResp> getInfo() {
        Long userId = AppUserContext.getUser().getUserId();
        return mobileUserService.getInfo(userId);
    }

}
